/*
 * Copyright (C) 2014 Realtek Semiconductor Corp.
 * All Rights Reserved.
 *
 * This program is the proprietary software of Realtek Semiconductor
 * Corporation and/or its licensors, and only be used, duplicated,
 * modified or distributed under the authorized license from Realtek.
 *
 * ANY USE OF THE SOFTWARE OTHER THAN AS AUTHORIZED UNDER
 * THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
 *
 * Purpose : Definition of OMCI IGNORE MIB table APIs
 *
 * Feature : The file includes the following modules and sub-modules
 *           (1) OMCI Ignore MIB table APIs
 */

/****************************************************************/
/* Include Files                                                */
/****************************************************************/
#include "app_basic.h"


/****************************************************************/
/* Symbol Definition                                            */
/****************************************************************/
#define OMCI_IGNORE_MIB_TBL_FILE_PATH "/var/config/omci_ignore_mib_tbl.conf"


/****************************************************************/
/* Type Definition                                              */
/****************************************************************/
typedef struct ignore_mib_table_s {
    unsigned int                    mibClassId;
    unsigned int                    subType;
    LIST_ENTRY(ignore_mib_table_s)  entries;
} __attribute__((packed)) ignore_mib_table_t;


/****************************************************************/
/* Data Declaration                                             */
/****************************************************************/
static LIST_HEAD(ignore_mib_table_head_s, ignore_mib_table_s) gIgnoreMibTable;


GOS_ERROR_CODE omci_ignore_mib_table_load_file (void) 
{
    char* line = NULL;
    FILE* ignoreMibCfgFp = NULL;
    size_t len =  0;
    ssize_t read;
    char* token = NULL;
    unsigned int meClassId   = 0xFFFF;
    unsigned int subType     = 0;
    ignore_mib_table_t	*pEntry = NULL;

    if (access(OMCI_IGNORE_MIB_TBL_FILE_PATH, F_OK) == -1)
    {
        printf("%s doesn't exist\n", OMCI_IGNORE_MIB_TBL_FILE_PATH);
        return GOS_FAIL ;
    }

    if ((ignoreMibCfgFp = fopen(OMCI_IGNORE_MIB_TBL_FILE_PATH, "r")) == NULL)
    {
        printf("%s: Failed to load %s\n", __FUNCTION__, OMCI_IGNORE_MIB_TBL_FILE_PATH);
        return GOS_FAIL ;
    }

    while ((read = getline (&line, &len, ignoreMibCfgFp)) != -1)
    {
        token = strtok (line, " ");

        if (!token || *token == '#' || *token == '\n')
        {
            continue;
        }
                 
        meClassId = strtol(token, NULL, 10); 
        printf("%s meClassId:%d\n", OMCI_IGNORE_MIB_TBL_FILE_PATH, meClassId);

        if(meClassId > 0xFFFF)
            continue;

        pEntry = malloc(sizeof(ignore_mib_table_t));
        if (!pEntry)
        	continue;

        pEntry->mibClassId = meClassId;

        token = strtok (NULL, " ");
        if (!token || *token == '#' || *token == '\n')
        {
            subType = 0;
        }
        else
            subType = strtol(token, NULL, 10); 
        
        pEntry->subType = subType;
        printf("%s subType:%d\n", OMCI_IGNORE_MIB_TBL_FILE_PATH, subType);
        
        LIST_INSERT_HEAD(&gIgnoreMibTable, pEntry, entries);          
    }

    return GOS_OK ;
}


GOS_ERROR_CODE omci_ignore_mib_table_check(UINT32 mibClassId, UINT32 subType, UINT8 *pIsIgnore)
{
    ignore_mib_table_t	*pEntry;

    if(!pIsIgnore)
        return GOS_ERR_PARAM;

    *pIsIgnore = 0;
    LIST_FOREACH(pEntry, &gIgnoreMibTable, entries)
    {
        if (pEntry->mibClassId == mibClassId && pEntry->subType == subType)
        {
            *pIsIgnore = 1;
            break;
            
        }
    }

    return GOS_OK;
}
